package com.chenyue.cm.payment.service.impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chenyue.cm.base.dao.LcBaseMapper;
import com.chenyue.cm.base.service.BaseServiceImpl;
import com.chenyue.cm.common.constant.RedisPrefix;
import com.chenyue.cm.common.utils.StringUtil;
import com.chenyue.cm.order.dao.JdOrderMapper;
import com.chenyue.cm.order.dao.JdProductTypeMapper;
import com.chenyue.cm.order.domain.JdOrder;
import com.chenyue.cm.order.domain.JdProduct;
import com.chenyue.cm.order.domain.JdProductType;
import com.chenyue.cm.payment.service.JdOrderService;
import com.chenyue.cm.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Calendar;

/**
 * @author LBX
 */
@Service(value = "JdOrderService")
public class JdOrderServiceImpl extends BaseServiceImpl<JdOrder> implements JdOrderService {

    private final JdOrderMapper jdOrderMapper;

    private final JdProductTypeMapper jdProductTypeMapper;

    private final RedisUtils redisUtils;

    public JdOrderServiceImpl(@Qualifier("jdOrderMapper") JdOrderMapper jdOrderMapper, @Qualifier("jdProductTypeMapper") JdProductTypeMapper jdProductTypeMapper, RedisUtils redisUtils) {
        this.jdOrderMapper = jdOrderMapper;
        this.jdProductTypeMapper = jdProductTypeMapper;
        this.redisUtils = redisUtils;
    }

    @Override
    @Resource(name = "jdOrderMapper")
    public void setBaseMapper(LcBaseMapper<JdOrder, Serializable> baseMapper) {
        this.baseMapper = baseMapper;
    }

    /**
     * 创建订单
     *
     * @param product  商品信息
     * @param buyCount 购买数量
     * @return 订单
     */
    @Override
    public JdOrder createOrder(JdProduct product, Integer buyCount, Integer userId) {
        JdProductType type = jdProductTypeMapper.selectById(product.getType());
        String orderNum = StringUtil.getOrderNum(type.getSign(), userId);
        JdOrder order = new JdOrder();
        order.setProductId(product.getId());
        order.setOrderNum(orderNum);
        order.setUserId(userId);
        order.setProductPrice(product.getSalePrice());
        order.setBuyCount(buyCount);
        order.setCt(Calendar.getInstance().getTime());
        order.setOrderStatus(0);
        int success = jdOrderMapper.insert(order);
        if (success > 0) {
//            7天临时订单
            final JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(order));
            jsonObject.put("type", product.getType());
            redisUtils.set(RedisPrefix.TEMP_ORDER + order.getId(), jsonObject, -1);
            return order;
        } else {
            return null;
        }
    }

    /**
     * 交易完成 订单核验
     *
     * @param orderId   订单id
     * @param tradeNo   三方交易号
     * @param payAmount 实际支付
     * @param userId    用户id
     */
    @Override
    public void tradeFinish(Integer orderId, String tradeNo, double payAmount, Integer userId) {
        JdOrder old = jdOrderMapper.selectOne(
                new LambdaQueryWrapper<JdOrder>()
                        .eq(JdOrder::getId, orderId)
                        .eq(JdOrder::getOrderStatus, 1)
        );
        if (old != null) {
//        已处理过 跳过
            return;
        }
        JdOrder order = new JdOrder();
        order.setId(orderId);
        order.setPayTime(Calendar.getInstance().getTime());
        order.setOrderStatus(1);
        order.setThirdTradeNum(tradeNo);
        order.setOrderAmount(payAmount / 100);
        jdOrderMapper.updateById(order);
    }
}
